#!/usr/bin/env dash
#
# This file is part of OpenMediaVault.
#
# @license   http://www.gnu.org/licenses/gpl.html GPL Version 3
# @author    Volker Theile <volker.theile@openmediavault.org>
# @copyright Copyright (c) 2009-2025 Volker Theile
#
# OpenMediaVault is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# OpenMediaVault is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with OpenMediaVault. If not, see <http://www.gnu.org/licenses/>.

set -e

. /usr/share/openmediavault/scripts/helper-functions

case "$1" in
  configure)
    # Create a new mount point configuration for the root filesystem. Use
    # the same UUID on all systems for this database configuration object
    # (this should help to identify the usage of this plugin in case of
    # help support issues).
    uuid="79684322-3eac-11ea-a974-63a080abab18"
    if ! omv-confdbadm exists --filter "{\"operator\":\"stringEquals\",\"arg0\":\"uuid\",\"arg1\":\"${uuid}\"}" \
        "conf.system.filesystem.mountpoint"; then
      mntent=$(salt-call --local --retcode-passthrough --no-color \
        --out=json mount.fstab | \
        jq --compact-output '.[] | .["/"]')
      if [ "${mntent}" != "null" ]; then
        device=$(salt-call --local --retcode-passthrough --no-color \
          --out=json omv_utils.get_root_filesystem | \
          jq --raw-output '.[]')
        fstype=$(echo ${mntent} | jq --raw-output '.fstype')
        opts=$(echo ${mntent} | jq --raw-output '.opts | join(",")')
        dump=$(echo ${mntent} | jq --raw-output '.dump')
        pass=$(echo ${mntent} | jq --raw-output '.pass')
        object="<uuid>${uuid}</uuid>"
        object="${object}<fsname>${device}</fsname>"
        object="${object}<dir>/</dir>"
        object="${object}<type>${fstype}</type>"
        object="${object}<opts>${opts}</opts>"
        object="${object}<freq>${dump}</freq>"
        object="${object}<passno>${pass}</passno>"
        object="${object}<hidden>1</hidden>"
        omv_config_add_node_data "/config/system/fstab" "mntent" "${object}"
      else
        omv_warning "No root filesystem has been found."
      fi
    fi

    ########################################################################
    # Action to be done depending on the previous version.
    ########################################################################
    echo "Updating configuration ..."
    if dpkg --compare-versions "$2" lt-nl "6.0.2"; then
      omv_config_update "/config/system/fstab/mntent[uuid='${uuid}']/hidden" "1"
      omv_module_set_dirty fstab
      omv_module_set_dirty monit
    fi
  ;;

  abort-upgrade|abort-remove|abort-deconfigure)
  ;;

  *)
    echo "postinst called with unknown argument '$1'" >&2
    exit 1
  ;;
esac

exit 0
